package com.synnex.mobile.servier.core.content.dao.impl;

import bsh.This;
import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.content.bo.ContentBo;
import com.synnex.mobile.servier.core.content.dao.ContentDao;
import com.synnex.mobile.servier.core.content.dto.Content;
import com.synnex.mobile.servier.core.content.dto.Groups;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * To change this template use File | Settings | File Templates.
 */
public class ContentHibernateImpl extends GenericHibernateDaoImpl<Content, Integer> implements ContentDao {


    public List<Content> getContentByType(String type) {

        Criteria c = this.getSession().createCriteria(Content.class);
        c.add(Restrictions.eq("type",type));
        c.addOrder(Order.asc("seq"));
        c.addOrder(Order.asc("id"));
        System.out.println("params: type="+type);
        return c.list();
    }

    public List<Content> getContent() {
        Criteria c = this.getSession().createCriteria(Content.class);
        List<String> types = new ArrayList<String>();
        types.add("food");
        types.add("view");
        types.add("tips");
        types.add("intro");
        c.add(Restrictions.in("type",types));
        return c.list();
    }

    public List<Content> getContentByGroup(Integer groupId) {

        Session session = this.getSession();

        String sql = "select gs.content_id from group_session gs where gs.group_id=:gid";
        List<Integer> contentIds = session.createSQLQuery(sql).setParameter("gid",groupId).list();

        if(contentIds==null){
            return null;
        }else{
            Criteria c = session.createCriteria(Content.class);
            c.add(Restrictions.in("id",contentIds));
            return c.list();
        }
    }

    public List<Content> getSessionByDayAndSection(Date date, String section) {
        Session session = this.getSession();

        Criteria c = session.createCriteria(Content.class);
        c.add(Restrictions.eq("type", "session"));

        if (date != null) {
            c.add(Restrictions.eq("day", date));
        }
        if (section != null && !section.equals("")) {
            c.add(Restrictions.eq("section", section));
        }
        c.addOrder(Order.asc("seq"));
        c.addOrder(Order.asc("day"));
        System.out.println("params: date=" + date + " ; section=" + section);
        return c.list();
    }

    public Content getCompanyIntro() {
        Criteria c = this.getSession().createCriteria(Content.class);
        c.add(Restrictions.eq("type","intro"));
        return (Content)c.uniqueResult();
    }

    public Content getContentById(Integer id) {
        return super.get(id);
    }

    public Groups getGroupById(Integer id) {
        Criteria c = this.getSession().createCriteria(Groups.class);
        c.add(Restrictions.eq("id",id));
        return (Groups)c.uniqueResult();
    }

    public List<Groups> getGroupList() {
        Criteria c = this.getSession().createCriteria(Groups.class);
        return c.list();
    }

    public void saveContent(Content content) {
        this.save(content);
    }

    public void editContent(Content content) {
        this.update(content);
    }

    public void deleteContentById(int id) {
        this.delete(get(id));
    }
}
